package edu.xmut.singleton.hungry;

/**
 * @author 朔风
 * @date 2024-02-16 12:52
 */
public class SingletonTest02 {
    public static void main(String[] args) {
        SingletonHungryCodeBlock instance1 = SingletonHungryCodeBlock.getInstance();
        SingletonHungryCodeBlock instance2 = SingletonHungryCodeBlock.getInstance();

        System.out.println(instance1 == instance2);
        System.out.println(instance1.hashCode() == instance2.hashCode());
    }
}

/**
 * 饿汉式单利模式（静态代码快）
 */
class SingletonHungryCodeBlock {
    // 1.私有的构造器
    private SingletonHungryCodeBlock() {
    }

    // 2.静态变量实例
    private static SingletonHungryCodeBlock instance;

    static {
        instance = new SingletonHungryCodeBlock();
    }

    // 3.提供一个类方法返回实例对象
    public static SingletonHungryCodeBlock getInstance() {
        return instance;
    }


}
